package com.yxlgateway.gateway.controller;


/**
 * @author: yxl
 * @since: 2021-12-27
 **/

import com.yxlcommon.common.pojo.RespBean;
import com.yxlgateway.service.AlipayService;
import com.yxlgateway.service.CallBackService;
import com.yxlgateway.service.PayService;
import com.yxlcommon.common.vo.PayVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/pay")
@Slf4j
public class AlipayController {

    @Autowired
    private AlipayService alipayService;

    @Autowired
    private PayService payService;

    @Autowired
    private CallBackService callBackService;


    @PostMapping("/aliPay")
    public RespBean<byte[]> alipay(@RequestBody PayVo payVo) throws Exception {
        if (payService.selectPayStatus(payVo.getSubject(), payVo.getOrderId()) == 1){
            return RespBean.operationFail(payVo.getSubject() + "已支付, 请勿重复支付！");
        }
        payService.saveOrder(payVo);
        return alipayService.aliPay(payVo);
    }

    @PostMapping("/callBack")
    public String callBack(HttpServletRequest servletRequest){
       return callBackService.callBack(servletRequest);
    }

}
